package com.pms.framework;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServlet;
/**
 * This is to create tomcat logs
 * 
 */
public class LogGenerator  extends HttpServlet {
	
	public void init(){     
		//code to print output to file
		Date sysDate = new Date();
		SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HHmmss");
		String logFile = System.getenv("CATALINA_HOME");
		
//	changes made for unix platform
		
		if(System.getenv("CATALINA_BASE")==null){
			
			if(System.getenv("CATALINA.BASE")==null){
				
				if(System.getenv("CATALINA_HOME")==null){
					logFile= System.getenv("CATALINA.HOME");
				}else{
					logFile= System.getenv("CATALINA_HOME");
				}
			}else{
				logFile = System.getenv("CATALINA.BASE");
			}
			
		}else{
			logFile = System.getenv("CATALINA_BASE");
		}
		
		
		if(logFile==null || logFile.equalsIgnoreCase("")){
			logFile="C:/defaultLog.log";
		}else{
			int lastIndex=logFile.lastIndexOf("\\");
			
			logFile=logFile.substring(0,lastIndex)+"/logs/"+logFile.substring(lastIndex)+format.format(sysDate)+".log";
			
		}
		
		try {
			File outFile = new File(logFile);
			
			PrintStream printStream = new PrintStream(new BufferedOutputStream(new FileOutputStream(outFile)), true);
			
			System.setOut(printStream);
			System.setErr(printStream);
			System.out.println("Log started: " + sysDate);
		} 
		
		catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
	}
}